package com.deepinjava.dbbmlite;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.sql.*;
import java.util.Map;
import java.util.Properties;

import static com.deepinjava.dbbmlite.Application.APP_PROP;

public class BenchDao {

    private Connection conn;

    public BenchDao() throws ClassNotFoundException, SQLException {

//        for (Map.Entry en : APP_PROP.entrySet()) {
//            System.out.println(en);
//        }

        Class.forName(APP_PROP.getProperty("class-name"));
        conn = DriverManager.getConnection(APP_PROP.getProperty("url"), APP_PROP.getProperty("username"), APP_PROP.getProperty("password"));

    }

    public int save(Benchresult benchresult) throws SQLException {
        String insResultSql = "INSERT INTO benchresult" +
                "(profile, dialect, dbproduct, driverinfo, totalrow, threatcount, totalmark, recordbody,dbversion,driverversion)" +
                "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        PreparedStatement ps = conn.prepareStatement(insResultSql, Statement.RETURN_GENERATED_KEYS);

        ps.setString(1,benchresult.getProfile());
        ps.setString(2,benchresult.getDialect());
        ps.setString(3,benchresult.getDbproduct());
        ps.setString(4,benchresult.getDriverinfo());
        ps.setLong(5,benchresult.getTotalrow());
        ps.setLong(6,benchresult.getThreatcount());
        ps.setLong(7,benchresult.getTotalmark());
        ps.setString(8,benchresult.getRecordbody());
        ps.setString(9,benchresult.getDbversion());
        ps.setString(10,benchresult.getDriverversion());
        ps.executeUpdate();

        ResultSet rs = ps.getGeneratedKeys();

        int sid=0;

        if (rs.next()) {
            sid =rs.getInt(1);
            benchresult.setId(sid);
            rs.close();
        }

        String insRecodSql = "INSERT INTO benchresult_item" +
                "(resultid, subject, mark, timecost, rowpm, detail)" +
                "VALUES(?, ?, ?, ?, ?, ?)";
        ps = conn.prepareStatement(insRecodSql);

        for (BenchresultItem rc: benchresult.getItems()) {
            rc.setResultid(sid);
            ps.setLong(1,rc.getResultid());
            ps.setString(2,rc.getSubject());
            ps.setLong(3,rc.getMark());
            ps.setLong(4,rc.getTimecost());
            ps.setLong(5,rc.getRowpm());
            ps.setString(6,rc.getDetail());

            ps.execute();
        }
        
        ps.close();
        conn.close();

        return sid;

    }

    public void post(BenchresultTrans trans) throws IOException, InterruptedException {
        String URI = "http://127.0.0.1:8080/add";
        System.out.println(trans.toJson());

        HttpRequest request = HttpRequest.newBuilder(java.net.URI.create(URI))
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(trans.toJson()))
                .build();

        HttpClient client = HttpClient.newBuilder().build();

//        HttpResponse<?> response = client.send(request, HttpResponse.BodyHandlers.discarding());
        HttpClient.newHttpClient()
                .sendAsync(request, HttpResponse.BodyHandlers.ofString())
                .thenApply(HttpResponse::statusCode)
                .thenAccept(System.out::println);

    }


}
